<template>
  <div>
    <el-table border :data="records" style="width: 100%">
      <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
      <el-table-column prop="skuName" label="名称" width="width"></el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width"></el-table-column>
      <el-table-column label="默认图片" width="150">
        <template slot-scope="{row}">
          <img :src="row.skuDefaultImg" style="width:80px;height:80px;">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量(KG)" width="80"></el-table-column>
      <el-table-column prop="price" label="价格(元)" width="80"></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="{row}">
          <el-button type="success" icon="el-icon-top" size="mini" v-if="row.isSale === 0"  @click="onSale(row)"></el-button>
          <el-button type="info" icon="el-icon-bottom" size="mini" v-else @click="cancelSale(row)"></el-button>   
          <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
          <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)"></el-button>
          <!-- 确认框 -->
          <el-popconfirm :title="`确定删除${row.skuName}吗`" @onConfirm="deleteSkuInfo(row)">
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      style="text-align:center;"
      :current-page="currentPage"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout=" prev, pager, next, jumper, -> ,sizes,total "
      :total="total"
      @size-change="handleSizeChange"
      @current-change="getSkuList"
    ></el-pagination>
    <!-- 抽屉 （侧边栏）-->
    <!-- 
      visible.sync: 是否展示
      show-close: 是否显示 关闭的那个 ×
      size: 控制抽屉大小
     -->
    <el-drawer
      :visible.sync="isShowDrawer"
      :show-close="false"
      size="50%">
      <el-row>
        <el-col :span="6"><div>名称</div></el-col>
        <el-col :span="16"><div>{{skuInfo.skuName}}</div></el-col>
      </el-row>
      <el-row>
        <el-col :span="6"><div>描述</div></el-col>
        <el-col :span="16"><div>{{skuInfo.skuDesc}}</div></el-col>
      </el-row>
      <el-row>
        <el-col :span="6"><div>价格</div></el-col>
        <el-col :span="16"><div>{{skuInfo.price}}</div></el-col>
      </el-row>
      <el-row>
        <el-col :span="6"><div>平台属性</div></el-col>
        <el-col :span="16">
          <span style="margin-right:10px;" v-for="attr in skuInfo.skuAttrValueList" :key="attr.id">
            <el-tag type="success">{{attr.attrName}}</el-tag>   
          </span>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6"><div>商品图片</div></el-col>
        <el-col :span="16">
          <div class="carousel">
            <el-carousel indicator-position="outside" arrow="always" interval="2000">
              <el-carousel-item height="400" v-for="item in skuInfo.skuImageList" :key="item.id">
                <img class="carousel_img" :src="item.imgUrl" alt="">
              </el-carousel-item>
            </el-carousel>
          </div>
        </el-col>
      </el-row>
      
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      currentPage: 1,
      limit: 3,
      total: 100,
      records:[],
      skuInfo:{},
      // 是否展示抽屉
      isShowDrawer:false
    };
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    // 获取SKU列表信息
    async getSkuList(pager = 1) {
      this.currentPage = pager
      let {currentPage,limit} = this
      // 请求SKU列表信息
      let result = await this.$api.sku.reqSkuList(currentPage,limit);
      if(result.code === 200){
        this.records = result.data.records
        this.total = result.data.total
      }
    },
    // 分页器页面变化时间
    handleSizeChange(limit){
      this.limit = limit
      this.getSkuList()
    },
    // SKU 上架
    async onSale(row){
      // 获取上架信息
      let result = await this.$api.sku.reqOnSale(row.id)
      if(result.code === 200){
        // isSale 控制按钮切换
        row.isSale = 1
        this.$message({message:'上架成功',type:'success'})
      }
    },
    // SKU下架
    async cancelSale(row){
      // 获取 下架 信息
      let result = await this.$api.sku.reqCancelSale(row.id)
      if(result.code === 200){
        row.isSale = 0
        this.$message({message:'下架成功',type:'warning'})

      }
    },
    // 获取Sku信息
    async getSkuInfo(row){
      this.isShowDrawer = true
      // 获取 Sku 信息
      let result = await this.$api.sku.reqSkuInfo(row.id)
      if(result.code === 200){
        this.skuInfo = result.data
      }
    },
    // 删除SKU信息
    async deleteSkuInfo(row){
      let result = await this.$api.sku.reqDeleteSkuInfo(row.id)
      console.log(result)
      if(result.code === 200){
        this.$message({message:'删除成功',type:'success'})
        this.getSkuList()
      }
      
    }
  }
};
</script>
<style>
  /* .el-carousel__item h3 {
    color: #475669;
    font-size: 18px;
    opacity: 0.75;
    line-height: 300px;
    margin: 0;
  } */
  
  /* .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  } */
  .el-carousel__button{
    width: 10px;
    height: 10px;
    background: #999 !important;
    border-radius: 50%;
  }
</style>

<style>
.el-col{
  margin: 10px;
}
.el-col-6{
  color: red;
  text-align: right;
  margin-right: 10px;
}
/* .carousel{

  width: 400px;
  height: 400px;
  background: red;
} */
.carousel_img{
  height: 100%;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}
</style>